package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdCreateService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.time.LocalDateTime;


/**
 * 新增广告的service实现
 * @author Meltedicecream
 * @since 2023/06/20 19:56
 */
public class AdminAdCreateServiceImpl implements AdminAdCreateService {
    @Override
    public MarketAd create(String name, String url, String content, String link, Byte position, Boolean enabled) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper marketAdMapper = session.getMapper(MarketAdMapper.class);
        //填充market对象
        MarketAd marketAd = new MarketAd();
        marketAd.setName(name);
        marketAd.setUrl(url);
        marketAd.setContent(content);
        marketAd.setLink(link);
        marketAd.setPosition(position);
        marketAd.setEnabled(enabled);
        LocalDateTime localAddTime = LocalDateTime.now();
        marketAd.setAddTime(localAddTime);
        marketAd.setUpdateTime(localAddTime);
        //将该对象写入数据库
        marketAdMapper.insert(marketAd);
        session.commit();
        session.close();
        //返回该对象
        return marketAd;
    }
}
